import { PageInfo, PostBrief, PostDetail } from '@/models';

//默认前台展示时间为createTime

export function convertPostList(
    data: any,
    isCreateTime = true
): Array<PostBrief> {
    const newsList = data.page_res.content;
    const postList: Array<PostBrief> = [];
    newsList.forEach((news: any) => {
        const postBrief: PostBrief = {
            pid: news.id,
            title: news.title,
            desc: news.description,
            datetime: isCreateTime ? news.createTime : news.updateTime,
            views: news.count,
            imgUrl: news.img,
        };
        postList.push(postBrief);
    });
    return postList;
}

export function convertPostDetail(data: any, isCreateTime = true): PostDetail {
    return {
        cid: data.news.catId,
        title: data.news.title,
        datetime: isCreateTime ? data.news.createTime : data.news.updateTime,
        views: data.news.count,
        from: data.news.authorName,
        content: data.newsContent.content,
    };
}

export function convertPageInfo(data: any): PageInfo {
    const pageResult = data.page_res;
    return {
        currentPage: pageResult.pageNum,
        rows: pageResult.totalSize,
        perPage: pageResult.pageSize,
    };
}

export function convertBannerList(data: any): Array<string> {
    return data.banners.map((banner: any) => {
        return {
            img: banner.image,
            title: banner.keywords,
            pid: banner.newsId,
        };
    });
}

export function convertStaffList(data: any) {
    return data.page_res;
}
